﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Threading;

namespace WeiSiCopy
{
    class Program
    {
        
        static int Main(string[] args)
        {
            if (new DBroot(BaseSetting.DBConn).ConnectionState)
            {
                ManualResetEvent eventX = new ManualResetEvent(false);
                ThreadPool.SetMaxThreads(BaseSetting.MaxThreadCount, int.MaxValue);

                List<Apply> applylist = null;

                Step step = new Step();
                step.CopyToTemp();
                applylist = step.TempToList();
                if (applylist != null)
                {
                    SiCopy scopy = null;
                    SiCopy.MaxCount = applylist.Count;
                    foreach (Apply apply in applylist)
                    {
                        scopy = new SiCopy(apply);
                        SiCopy.eventX = eventX;
                        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), scopy);
                        System.Threading.Thread.Sleep(1000);

                    }
                    eventX.WaitOne(Timeout.Infinite, true);
                }
            }
            else
            {
                Log.WriteLog("連結資料庫失敗! " + BaseSetting.DBConn, true);
            }
            return 0;

        }
        static void ThreadProc(Object stateInfo)
        {
            SiCopy sp = (SiCopy)stateInfo;
            sp.Run();
        }
        
    }
}
